<script setup>
import { storeToRefs } from 'pinia'
import { useAppStore } from '../store'
import { CaretBackOutline as CashIcon, SettingsOutline as SettingsIcon ,Add} from "@vicons/ionicons5";
import { onMounted } from "vue";

const store = useAppStore()
const { isAltDown,input,info,taskLog,loading,isReady,taskQueue,isCommand,options,inputRef,isNoDrag,showModal,addValue} = storeToRefs(store)
const { connect ,sendMessage,handleSelect,onInput,showCodePage} = store

onMounted(() => {
  connect()

  function handleKeyDown(event) {
    if (event.key === 'Alt'  ) {
      isAltDown.value = false
    }
  }
  function handleKeyUp(event) {
    if ( event.key === 'Alt' ) {
      isAltDown.value = true
    }
  }

  window.addEventListener('keyup', handleKeyUp)
  window.addEventListener('keydown', handleKeyDown)
})



</script>

<template>
  <div id="container" class="container">
    <div class="bg">
        <div class="bg-left">
          <n-button class="setting" text @click="showCodePage()">
            <n-icon color="#000" size="14" >
              <SettingsIcon />
            </n-icon>
          </n-button>
        </div>
        <div class="bg-middle">
          <n-dropdown
              style="width: 100%"
              :options="options"
              :show="isCommand"
              placement="bottom-start"
              size="small"
              @select="handleSelect">
            <input ref="inputRef" v-model="input" class="user-input" @input="onInput"  @keydown.enter="sendMessage" />
          </n-dropdown>
<!--          <n-select-->
<!--              class="user-input"-->
<!--              :options="options"-->
<!--              :filterable="true"-->
<!--              :consistent-menu-width="true"-->
<!--          />-->
<!--          <n-auto-complete-->
<!--              class="user-input"-->
<!--              v-model="input"-->
<!--              :options="options"-->
<!--              placeholder="按下 “/” 调出指令列表"-->
<!--              :get-show="getShow"-->
<!--          />-->
<!--          <n-mention class="user-input"-->
<!--                     :options="options"-->
<!--                     :prefix="['/', '、']"-->
<!--                     @search="sendMessage"-->
<!--                     @input="onInput"-->
<!--                     v-model:value="input"/>-->
        </div>
      <div class="bg-right">
        <template v-if="isAltDown">
          <n-popselect v-model:value="addValue" :options="options" trigger="click" >
            <n-button class="add" color="#FFF"  >
              <n-icon color="#000" size="22">
                <Add />
              </n-icon>
            </n-button>
          </n-popselect>
        </template>
        <template v-else>
          <n-button class="add" color="#FFF" size="tiny" >
            <n-icon color="#000" size="22">
              <svg viewBox="0 0 512 512" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"><path d="M160 368L32 256l128-112" fill="none" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="32"></path><path d="M352 368l128-112l-128-112" fill="none" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="32"></path><path d="M304 96l-96 320" fill="none" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="32"></path></svg>
            </n-icon>
          </n-button>
        </template>
      </div>
    </div>


    <div :class="{'no-drag':isNoDrag}" class="info">
      <div class="info-left">
        <div :class="[isReady ? 'green' : 'red' , {'loading' : loading}]"></div>
      </div>
      <div class="info-min">
        {{ info }}
      </div>
      <div class="info-right no-drag">
        <n-button style="font-size: 18px" text>
          <n-icon>
            <CashIcon />
          </n-icon>
        </n-button>
      </div>
      <div class="info-bottom no-drag">
        <template v-for="task in taskLog" :key="task" style="text-align: center;height: 20px">
          <div style="display: flex;flex-direction: row;">
            <div class="green" style="margin: 8px 0 0 5px;"></div>
            <div style="height: 25px;font-size: 13px;margin-left: 15px;">{{ task.info }}</div>
          </div>


        </template>
      </div>
    </div>

  </div>



</template>

<style scoped>
@keyframes bounce {
  0% { transform: translateX(00px) scale(0.7); }
  25% { transform: translateX(10px) scale(1); }
  50% { transform: translateX(0px) scale(0.7); }
  75% { transform: translateX(-10px) scale(1); }
  100% { transform: translateX(0px) scale(0.7); }
}

.red{
  width: 8px;
  height: 8px;
  background-color: rgba(255, 60, 0, 0.78);
  border-radius: 50%;
}

.green{
  width: 8px;
  height: 8px;
  background-color: green;
  border-radius: 50%;
  align-items: center;
}

.loading{
  width: 10px;
  height: 10px;
  border-radius: 50%;
  animation: bounce 1.5s infinite;
  animation-timing-function: cubic-bezier(.69,.13,.3,.86)
}

.setting{
  -webkit-app-region: no-drag;
}

.user-input{
  display: flex;
  height: 32px;
  border: none;
  padding: 0.5rem;
  font-size: 14px;
  box-sizing: border-box;
  border-radius: 5px;
  -webkit-app-region: no-drag;
  width: 100%;
}

.user-input:focus{
  outline: none;
}

.container{
  display: flex;
  width: 100%;
  flex-direction: column;
  background-color: #F6F8FA;
  border-radius: 5px;
  padding: 5px 5px 0 5px;
  box-sizing: border-box;
}

.bg{
  width: 100%;
  display: grid;
  grid-template-columns: 30px 1fr 30px;
  grid-template-rows: 1fr;
  grid-template-areas:
    "bg-left bg-middle bg-right";
  gap: 0 5px;
  background-color: #C5CEDB;
  border-radius: 5px;
  padding: 5px;
  box-sizing: border-box;
}


.bg-left{
  grid-area: bg-left;
}
.bg-middle{
  grid-area: bg-middle;
}
.bg-right{
  grid-area: bg-right;
}

.add{
  -webkit-app-region: no-drag;
}

.info{
  display: grid;
  grid-template-columns: 20px 1fr 42px;
  grid-template-rows: 30px 1fr;
  gap: 0 0;
  grid-auto-flow: row;
  grid-template-areas:"info-left info-min info-right"
"info-bottom info-bottom info-bottom";
  padding-left: 10px;
  align-items:center;
  box-sizing: border-box;
  overflow: hidden;
  max-height: 400px;
}

.info-left{
  grid-area: info-left;
  display:flex;
  justify-content: center;
}

.info-min{
  grid-area: info-min;
  display:flex;
  padding-left: 9px;
  font-size: 13px;
}

.info-right{
  grid-area: info-right;
  display:flex;
  justify-content: flex-end;
}
.info-right :hover{
  grid-area: info-right;
  display:flex;
  justify-content: flex-end;
  color: #d000ff;
}

.info-bottom{
  grid-area: info-bottom;
  flex-direction: column;
  gap: 10px;
  align-items: center;
  justify-content: center;
}

</style>
